import Cookies from 'js-cookie'
import store from "@/store/index";

const Token = 'Token'
const TokenKey = 'TokenKey'
const RefreshToken = 'RefreshToken'
const DefaultPath = 'defaultPath'

// 获取token
export function getToken() {
	console.log(Cookies.get(Token))
  return Cookies.get(Token)
}

// 设置token
export function setToken(token) {
  return Cookies.set(Token, token)
}

// 删除token
export function removeToken() {
  return Cookies.remove(Token)
}

// 获取tokenKey
export function getTokenKey() {
  return Cookies.get(TokenKey)
}

// 设置tokenKey
export function setTokenKey(tokenKey) {
  return Cookies.set(TokenKey, tokenKey)
}

// 删除tokenKey
export function removeTokenKey() {
  return Cookies.remove(TokenKey)
}

// 获取refreshToken
export function getRefreshToken() {
  return Cookies.get(RefreshToken)
}

// 设置refreshToken
export function setRefreshToken(refreshToken) {
  return Cookies.set(RefreshToken, refreshToken)
}

// 删除refreshToken
export function removeRefreshToken() {
  return Cookies.remove(RefreshToken)
}

// 获取defaultPath
export function getDefaultPath() {
  return Cookies.get(DefaultPath)
}

// 设置defaultPath
export function setDefaultPath(defaultPath) {
  return Cookies.set(DefaultPath, defaultPath)
}

// 删除defaultPath
export function removeDefaultPath() {
  return Cookies.remove(DefaultPath)
}

/**
 * 获取权限
 */
export function hasPermi(value) {
  //获取cookie用户信息
  let userInfo = store.getters.info;
  //定义默认无权限
  let isExist = false;
  //判断是否存在用户权限
  if (userInfo && userInfo.permissions) {
    //定义默认所有权限

    const all_permission = "*:*:*";
    //获取当前按钮权限
    const permissions = userInfo.permissions;
    //遍历所有对比权限值
    if (value && value instanceof Array && value.length > 0) {
      //定义临时值
      const permissionFlag = value;
      //循环遍历对比权限
      const hasPermissions = permissions.some((permission) => {
        //如果找到权限
        let temp =
          all_permission === permission || permissionFlag.includes(permission);
        //判断找到权限进行显示
        if (temp) {
          isExist = true;
        }
      });
    }
  }
  return isExist;
}

export function getShow(value) {
  //默认按钮显示
  let isShow = true;
  if (value == "1") {
    isShow = true;
  } else if (value == "2") {
    isShow = false;
  } else {
    isShow = false;
  }
  return isShow;
}
